import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_study_app/widget/my_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:intl/intl.dart';

class CaseClearDetailPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _CaseClearDetailPageState();
}

class _CaseClearDetailPageState extends State<CaseClearDetailPage> {
  bool _isFinish = false;
  int _isExecute = -1;
  int _currentProgress = 1;
  bool _isForceExecute = false; //是否强制执行
  final List<String> _progressList = [
    '立案清算',
    '开庭判决时间',
    '判决生效时间',
    '立案执行',
    '强制执行'
  ];
  List<String> _labelList = ['2021-11-03', '', '', '已执行', ''];

  _selectDate(int nodeIndex) {
    DatePicker.showDatePicker(context,
        locale: LocaleType.zh,
        showTitleActions: true,
        minTime: DateTime.now(),
        theme: DatePickerTheme(doneStyle: TextStyle(color: Color(0xFF009669))),
        onConfirm: (date) {
      _labelList[nodeIndex] = formatDate(date, [yyyy, '-', mm, '-', dd]);
      setState(() {});
    });
  }

  _buildProgressItemView(int nodeIndex) {
    bool isFinished = nodeIndex < _currentProgress;

    bool isShowExecute = _currentProgress == 3 && nodeIndex == _currentProgress;

    bool isShowForceExecute = (_currentProgress == 4 && _isForceExecute);
    bool isShowInputTime = (_currentProgress == 1 || _currentProgress == 2) &&
        nodeIndex == _currentProgress;
    isShowInputTime = isShowInputTime || (isShowForceExecute && nodeIndex == 4);

    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: 12.w,
          ),
          Container(
            width: 20.w,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.asset(
                  isFinished
                      ? 'assets/images/ic_checked.png'
                      : 'assets/images/ic_check_no.png',
                  width: 20.w,
                  height: 20.w,
                ),
                Container(
                  width: 1.w,
                  constraints: BoxConstraints(
                    maxHeight: 45.w,
                  ),
                  color: isFinished ? Color(0xFF009669) : Color(0xFFEEEEEE),
                )
              ],
            ),
          ),
          SizedBox(
            width: 12.w,
          ),
          Expanded(
            child: Container(
              child: Column(
                children: [
                  Row(
                    children: [
                      Text(
                        _progressList[nodeIndex],
                        style: TextStyle(
                            fontSize: 14.sp,
                            color: isFinished
                                ? Color(0xFF333333)
                                : Color(0xFF999999)),
                      ),
                      SizedBox(
                        width: 10.w,
                      ),
                      Offstage(
                        offstage: !isShowInputTime,
                        child: InkWell(
                          onTap: () {
                            _selectDate(nodeIndex);
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 15.w),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10.w),
                                color: Color(0xFF009669)),
                            child: Text(
                              '录入时间',
                              style: TextStyle(
                                  fontSize: 12.sp, color: Colors.white),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Offstage(
                    offstage: isShowExecute,
                    child: Container(
                      margin: EdgeInsets.only(top: 6.w),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        isFinished || nodeIndex == _currentProgress
                            ? _labelList[nodeIndex]
                            : '',
                        style: TextStyle(
                            fontSize: 14.sp,
                            color: isFinished
                                ? Color(0xFF333333)
                                : Color(0xFF999999)),
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: !isShowExecute,
                    child: Container(
                      margin: EdgeInsets.only(top: 8.w),
                      child: Row(
                        children: [
                          InkWell(
                            onTap: () {
                              _isExecute = 1;
                              setState(() {});
                            },
                            child: Container(
                              padding: EdgeInsets.only(right: 20.w),
                              child: Row(
                                children: [
                                  Image.asset(
                                    _isExecute == 1
                                        ? 'assets/images/ic_single_checked.png'
                                        : 'assets/images/ic_single_check_no.png',
                                    width: 16.w,
                                    height: 16.w,
                                  ),
                                  SizedBox(
                                    width: 8.w,
                                  ),
                                  Text(
                                    '已执行',
                                    style: TextStyle(
                                        fontSize: 14.sp,
                                        color: Color(0xFF333333)),
                                  )
                                ],
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              _isExecute = 0;
                              setState(() {});
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 20.w),
                              child: Row(
                                children: [
                                  Image.asset(
                                    _isExecute == 0
                                        ? 'assets/images/ic_single_checked.png'
                                        : 'assets/images/ic_single_check_no.png',
                                    width: 16.w,
                                    height: 16.w,
                                  ),
                                  SizedBox(
                                    width: 8.w,
                                  ),
                                  Text(
                                    '未执行',
                                    style: TextStyle(
                                        fontSize: 14.sp,
                                        color: Color(0xFF333333)),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Offstage(
                    offstage: !(nodeIndex == 4 && isShowForceExecute),
                    child: Container(
                      margin: EdgeInsets.only(top: 6.w),
                      child: Row(
                        children: [
                          Container(
                            width: 60.w,
                            height: 25.w,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(
                                    color: Color(0xFF979797), width: 1.w)),
                            child: TextField(
                              keyboardType: TextInputType.number,
                              textAlign: TextAlign.center,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                disabledBorder: InputBorder.none,
                              ),
                              style: TextStyle(
                                  fontSize: 14.sp, color: Color(0xFF999999)),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 8.w),
                            child: Text(
                              '湘',
                              style: TextStyle(
                                  color: Color(0xFF333333), fontSize: 14.sp),
                            ),
                          ),
                          Container(
                            width: 60.w,
                            height: 25.w,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(
                                    color: Color(0xFF979797), width: 1.w)),
                            child: TextField(
                              keyboardType: TextInputType.number,
                              textAlign: TextAlign.center,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                disabledBorder: InputBorder.none,
                              ),
                              style: TextStyle(
                                  fontSize: 14.sp, color: Color(0xFF999999)),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 8.w),
                            child: Text(
                              '执',
                              style: TextStyle(
                                  color: Color(0xFF333333), fontSize: 14.sp),
                            ),
                          ),
                          Container(
                            width: 60.w,
                            height: 25.w,
                            decoration: BoxDecoration(
                                color: Colors.white,
                                border: Border.all(
                                    color: Color(0xFF979797), width: 1.w)),
                            child: TextField(
                              keyboardType: TextInputType.number,
                              textAlign: TextAlign.center,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                disabledBorder: InputBorder.none,
                              ),
                              style: TextStyle(
                                  fontSize: 14.sp, color: Color(0xFF999999)),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.symmetric(horizontal: 8.w),
                            child: Text(
                              '号',
                              style: TextStyle(
                                  color: Color(0xFF333333), fontSize: 14.sp),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 10.w,
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: '立案清算',
      ),
      body: Column(
        children: [
          Container(
            color: Color(0xFFEEEEEE),
            height: 1.w,
          ),
          Expanded(
            child: ListView(
              children: [
                Card(
                  elevation: 3,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.w)),
                  ),
                  color: Colors.white,
                  margin: EdgeInsets.all(10.w),
                  child: Column(
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(
                            horizontal: 12.w, vertical: 10.w),
                        child: Row(
                          children: [
                            Text(
                              '(2021)湘0702民初01025号',
                              style: TextStyle(
                                  fontSize: 14.sp, color: Color(0xFF666666)),
                            ),
                            Spacer(),
                            !_isFinish
                                ? GestureDetector(
                                    onTap: () {},
                                    child: Container(
                                      padding: EdgeInsets.symmetric(
                                          horizontal: 15.w, vertical: 1.w),
                                      decoration: BoxDecoration(
                                          borderRadius:
                                              BorderRadius.circular(12.w),
                                          border: Border.all(
                                              width: 1.w,
                                              color: Color(0xFFE02020))),
                                      child: Text(
                                        '撤销',
                                        style: TextStyle(
                                            fontSize: 13.sp,
                                            color: Color(0xFFE02020)),
                                      ),
                                    ),
                                  )
                                : Text(
                                    '已完结',
                                    style: TextStyle(
                                        color: Color(0xFF333333),
                                        fontSize: 14.sp),
                                  )
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(bottom: 8.w),
                        height: 1.w,
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        color: Color(0xFFEEEEEE),
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        child: Row(
                          children: [
                            Text(
                              '张三',
                              style: TextStyle(
                                  fontSize: 14.sp, color: Color(0xFF333333)),
                            ),
                            Spacer(),
                            Text(
                              '￥2553.23',
                              style: TextStyle(
                                  fontSize: 16.sp,
                                  color: Color(0xFF333333),
                                  fontWeight: FontWeight.bold),
                            )
                          ],
                        ),
                      ),
                      SizedBox(
                        height: 2.w,
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        child: Text(
                          '测试一下',
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: Color(0xFF333333),
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 2.w,
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '合同编号：TEST01',
                          style: TextStyle(
                              fontSize: 14.sp, color: Color(0xFF333333)),
                        ),
                      ),
                      SizedBox(
                        height: 2.w,
                      ),
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 12.w),
                        alignment: Alignment.centerLeft,
                        child: Text(
                          '借据编号：TEST02',
                          style: TextStyle(
                              fontSize: 14.sp, color: Color(0xFF333333)),
                        ),
                      ),
                      SizedBox(
                        height: 12.w,
                      ),
                    ],
                  ),
                ),
                Card(
                  elevation: 3,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8.w)),
                  ),
                  color: Colors.white,
                  margin: EdgeInsets.all(10.w),
                  child: Container(
                      padding: EdgeInsets.symmetric(vertical: 12.w),
                      child: ListView.builder(
                          itemCount: 5,
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemBuilder: (context, index) {
                            return _buildProgressItemView(index);
                          })),
                )
              ],
            ),
          ),
          Offstage(
            offstage: false,
            child: SafeArea(
              child: Container(
                padding: EdgeInsets.all(12.w),
                width: double.infinity,
                height: 74.w,
                color: Colors.white,
                child: TextButton(
                  style: ButtonStyle(
                    backgroundColor:
                        MaterialStateProperty.all(Color(0xff009868)),
                  ),
                  onPressed: () {},
                  child: Text(
                    '提交',
                    style: TextStyle(color: Colors.white, fontSize: 18.sp),
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
